package com.what21.netfwd;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class NetServer {

	final HashMap<AddressInfo, AddressInfo> mappings = new HashMap<AddressInfo, AddressInfo>();

	public NetServer(ForwardMapping mapping) {
		this(mapping.toMapping());
	}

	public NetServer(Map<AddressInfo, AddressInfo> map) {
		Set<Entry<AddressInfo, AddressInfo>> entrySet = map.entrySet();
		Iterator<Entry<AddressInfo, AddressInfo>> it = entrySet.iterator();
		while (it.hasNext()) {
			Entry<AddressInfo, AddressInfo> next = it.next();
			AddressInfo key = next.getKey();
			AddressInfo value = next.getValue();
			ForwardUtil.debug("src : " + key + " <-> desc : " + value);
			this.mappings.put(key, value);
		}
	}

	public void start() {
		Set<Entry<AddressInfo, AddressInfo>> entrySet = this.mappings.entrySet();
		Iterator<Entry<AddressInfo, AddressInfo>> it = entrySet.iterator();
		while (it.hasNext()) {
			Entry<AddressInfo, AddressInfo> next = it.next();
			AddressInfo key = next.getKey();
			AddressInfo value = next.getValue();
			new TCPServer(key,value).start();
		}
	}

}